package com.hzz.framework.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class PageDto<T> {

    /**
     * 当前第几页
     */
    int pageIndex = 0;
    /**
     * 每页数据条数
     */
    int pageSize = 10;
    /**
     * 数据总数
     */
    int dataTotal = 0;
    /**
     * 总页数
     */
    int pageCount;
    /**
     * 上一页
     */
    int prePage;
    /**
     * 下一页
     */
    int nextPage;

    List<T> data = new ArrayList<>(0);
    /**
     * 本次数量
     */
    int dataCount = 0;

    /**
     * 最后查询时间
     */
    Date lasttime;

    Map<String, Object> otherField = null;

    public PageDto() {
    }

    public PageDto(int pageIndex, int pageSize, List data) {
        this(pageIndex, pageSize, -1, data);
    }

    public PageDto(int pageIndex, int pageSize, int dataCount, List data) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.dataTotal = dataCount;
        this.lasttime = new Date();
        this.setData(data);
    }

    public void setData(List data) {
        this.data = data == null ? new ArrayList<>(0) : data;
        this.dataCount = this.data.size();
    }

    public void addField(String key, Object obj) {
        if (otherField == null) {
            otherField = new ConcurrentHashMap<>();
        }
        otherField.put(key, obj);
    }

    public void removeField(String key) {
        otherField.remove(key);
    }
}
